



from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# 假设这里有一些标记好的数据，包括文本和标签（0表示正常，1表示网络钓鱼）
texts = ["这是一条正常信息", "点击链接获取免费礼品", "确认您的银行账号信息", "你的账户需要验证，请点击链接"]
labels = [0, 1, 1, 1]

# 将文本转换成词频矩阵
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)

# 将数据分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.2, random_state=42)

# 构建朴素贝叶斯分类器
classifier = MultinomialNB()
classifier.fit(X_train, y_train)

# 在测试集上进行预测
y_pred = classifier.predict(X_test)

# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("模型准确率：", accuracy)

# 示例用于预测新文本
new_text = ["点击此链接获得免费奖品"]
new_text_vectorized = vectorizer.transform(new_text)
prediction = classifier.predict(new_text_vectorized)
print("新文本预测结果：", prediction)



